package com.example.springcloud.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.example.springcloud.domain.Path;
import com.example.springcloud.mapper.RouteMapper;
import com.example.springcloud.domain.Route;
import com.example.springcloud.service.IRouteService;

/**
 * 路线Service业务层处理
 * 
 * @author helian
 * @date 2025-03-01
 */
@Service
public class RouteServiceImpl implements IRouteService 
{
    @Autowired
    private RouteMapper routeMapper;

    /**
     * 查询路线
     * 
     * @param id 路线主键
     * @return 路线
     */
    @Override
    public Route selectRouteById(Long id)
    {
        return routeMapper.selectRouteById(id);
    }

    /**
     * 查询路线列表
     * 
     * @param route 路线
     * @return 路线
     */
    @Override
    public List<Route> selectRouteList(Route route)
    {
        return routeMapper.selectRouteList(route);
    }

    /**
     * 新增路线
     * 
     * @param route 路线
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRoute(Route route)
    {
        int rows = routeMapper.insertRoute(route);
        insertPath(route);
        return rows;
    }

    /**
     * 修改路线
     * 
     * @param route 路线
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRoute(Route route)
    {
        routeMapper.deletePathByRouteId(route.getId());
        insertPath(route);
        return routeMapper.updateRoute(route);
    }

    /**
     * 批量删除路线
     * 
     * @param ids 需要删除的路线主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRouteByIds(Long[] ids)
    {
        routeMapper.deletePathByRouteIds(ids);
        return routeMapper.deleteRouteByIds(ids);
    }

    /**
     * 删除路线信息
     * 
     * @param id 路线主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRouteById(Long id)
    {
        routeMapper.deletePathByRouteId(id);
        return routeMapper.deleteRouteById(id);
    }

    /**
     * 新增途径点信息
     * 
     * @param route 路线对象
     */
    public void insertPath(Route route)
    {
        List<Path> pathList = route.getPathList();
        Long id = route.getId();
        if (StringUtils.isNotNull(pathList))
        {
            List<Path> list = new ArrayList<Path>();
            for (Path path : pathList)
            {
                path.setRouteId(id);
                list.add(path);
            }
            if (list.size() > 0)
            {
                routeMapper.batchPath(list);
            }
        }
    }
}
